package com.sandking.db.jedis;

import java.util.ArrayList;
import com.sandking.db.bean.Yhrw;
import com.sandking.db.dao.YhrwDao;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import java.util.Set;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;

public class YhrwJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Yhrw getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhrw yhrw = null;
		try{
			String key = "Yhrw_Object";
			String field = SK_Plus.b("id:",id).e();
			yhrw = Yhrw.createForJson(jedis.hget(key,field));
			if(yhrw == null){
				yhrw = YhrwDao.getById(id);
				if(yhrw!=null){
					loadCache(yhrw);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhrw;
	}
	
	/**
	 * 根据( 用户_id  任务id ) 查询
	 */
	public static Yhrw getByYh_idRwid(int yh_id, int rwid){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhrw yhrw = null;
		try{
			String key = "Yhrw_Index";
			String field = SK_Plus.b("yh_id, rwid:",yh_id, rwid).e();
			String primaryKey = jedis.hget(key,field);
			if(primaryKey!=null){
				yhrw = getById(Integer.valueOf(primaryKey));
			}
			if(yhrw == null){
				yhrw = YhrwDao.getByYh_idRwid(yh_id, rwid);
				if(yhrw!=null){
					loadCache(yhrw);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhrw;
	}
	
	/**
	 * 根据( 用户_id ) 查询
	 */
	public static List<Yhrw> getByYh_id(int yh_id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Yhrw> yhrws = new ArrayList<Yhrw>();
		try{
			String key = "Yhrw_Index";
			String field = SK_Plus.b(key,"yh_id:",yh_id).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Yhrw_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				yhrws = Yhrw.createForJson(jsons);
			}else{
				yhrws = YhrwDao.getByYh_id(yh_id);
				if(yhrws!=null && !yhrws.isEmpty()){
					loadCaches(yhrws);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return yhrws;
	}
	

	public static void loadCache(Yhrw yhrw){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(yhrw,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Yhrw yhrw,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhrw_Object";
		String field = SK_Plus.b("id:",yhrw.getId()).e();
		String data = yhrw.toJson();
		p.hset(key,field,data);
		
		// ---------------------- 唯一索引 ----------------------
		String primaryKey = String.valueOf(yhrw.getId());
		key = "Yhrw_Index";
		
		field = SK_Plus.b("yh_id, rwid:",yhrw.getYh_id(),yhrw.getRwid()).e();
		p.hset(key,field,primaryKey);
		
		// ---------------------- 聚集索引 ----------------------
		field = SK_Plus.b(key,"yh_id:",yhrw.getYh_id()).e();
		p.sadd(field, primaryKey);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Yhrw> yhrws){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhrw yhrw : yhrws){
				loadCache(yhrw,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Yhrw yhrw){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(yhrw,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Yhrw yhrw,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhrw_Object";
		String field = SK_Plus.b("id:",yhrw.getId()).e();
		p.hdel(key,field);
		
		// ---------------------- 唯一索引 ----------------------
		key = "Yhrw_Index";
		
		field = SK_Plus.b("yh_id, rwid:",yhrw.getYh_id(),yhrw.getRwid()).e();
		p.hdel(key,field);
		
		// ---------------------- 聚集索引 ----------------------
		String primaryKey = String.valueOf(yhrw.getId());
		field = SK_Plus.b(key,"yh_id:",yhrw.getYh_id()).e();
		p.srem(field, primaryKey);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Yhrw> yhrws){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhrw yhrw : yhrws){
				clearCache(yhrw,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Yhrw insert(Yhrw yhrw){
		yhrw = YhrwDao.insert(yhrw);
		if(yhrw!=null){
			loadCache(yhrw);
		}
    	return yhrw;
    }
    
    public static Yhrw update(Yhrw yhrw){
    	yhrw = YhrwDao.update(yhrw);
    	if(yhrw!=null){
			loadCache(yhrw);
		}
    	return yhrw;
    }
    
    public static boolean delete(Yhrw yhrw){
    	boolean bool = YhrwDao.delete(yhrw);
    	if(bool){
    		clearCache(yhrw);
    	}
    	return bool;
    }
}